//
//  DCOrderDetailViewController.m
//  Driver
//
//  Created by 大橙子 on 2018/11/6.
//  Copyright © 2018 Tomous. All rights reserved.
//

#import "DCOrderDetailViewController.h"
#import "DCOrderDetailCell.h"
#import "DCGisticsView.h"
#import "DCGisticsModel.h"
#import "DCOrderDetailModel.h"
@interface DCOrderDetailViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataSource;
@property (nonatomic,strong) UIView *tableHeaderView;
@property (nonatomic,copy) NSString *orderID;
@property (nonatomic,strong) DCOrderDetailModel *model;
@property (nonatomic,copy) NSString *state;
@end

@implementation DCOrderDetailViewController
-(instancetype)initWithOrderID:(NSString *)orderID state:(NSString *)state
{
    if (self = [super init]) {
        self.orderID = orderID;
        self.state = state;
    }
    return self;
}
-(NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"订单详情";
    
    [self getOrderDetail];
}
-(void)getOrderDetail {
    NSMutableDictionary *info = [NSMutableDictionary dictionary];
    info[@"orderId"] = self.orderID;
    __weak typeof(self) weakself = self;
    [DCServiceTool postWithUrl:getPagerTasksByCarrier01Details_URL params:info success:^(id responseObject) {
        
        if ([responseObject[@"code"] integerValue] == 0) {
            weakself.model = [DCOrderDetailModel mj_objectWithKeyValues:responseObject[@"record"]];
                [weakself setUpMainView];
        }else{
            [weakself setUpBgViewIsShow:YES block:^{
                [weakself getOrderDetail];
            }];
            [weakself showMessage:responseObject[@"msg"] afterDelay:1.0];
        }
    } failure:^(NSError *error) {
        
    }];
}
-(void)setUpMainView
{
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:self.view.bounds];
    scrollView.backgroundColor = BackgroundColor;
    [self.view addSubview:scrollView];
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 5, self.view.width, 50)];
    titleLabel.text = Str_IsEmpty(self.model.taskCode)?@"taskCode":self.model.taskCode;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = FONT(25);
    titleLabel.textColor = BlueColor;
    titleLabel.backgroundColor = [UIColor whiteColor];
    [scrollView addSubview:titleLabel];

    UIView *firstView = [[UIView alloc]initWithFrame:CGRectMake(0,titleLabel.bottom + 5, self.view.width, 80)];
    firstView.backgroundColor = [UIColor whiteColor];
    [scrollView addSubview:firstView];

    UIImageView *startImageView = [[UIImageView alloc]initWithImage:IMAGENAME(@"起点")];
    startImageView.frame = CGRectMake(20, 5, 30, 30);
    [firstView addSubview:startImageView];

    UITextField *startField = [[UITextField alloc]initWithFrame:CGRectMake(startImageView.right+5, 0, firstView.width-20-startImageView.width - 10, firstView.height/2)];
//    startField.clearButtonMode = UITextFieldViewModeAlways;
    DCOrderDetailOdd *odd = [DCOrderDetailOdd new];
    if (self.model.oddArray.count) {
        odd = self.model.oddArray[0];
    }
    startField.text = odd.startAddress;
    startField.enabled = NO;
    [firstView addSubview:startField];

    UIImageView *endImageView = [[UIImageView alloc]initWithImage:IMAGENAME(@"终点")];
    endImageView.frame = CGRectMake(20, startField.bottom+5, 30, 30);
    [firstView addSubview:endImageView];

    UITextField *endField = [[UITextField alloc]initWithFrame:CGRectMake(startField.x, startField.bottom, startField.width, startField.height)];
//    endField.clearButtonMode = UITextFieldViewModeAlways;
    endField.text = [NSString stringWithFormat:@"%@%@",odd.endCity,odd.endAddress];
    endField.enabled = NO;
    [firstView addSubview:endField];

    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(startField.x - 5, startField.bottom-0.5, startField.width, 1)];
    line.backgroundColor = [UIColor lightGrayColor];
    [firstView addSubview:line];

    UIView *secondView = [[UIView alloc]initWithFrame:CGRectMake(0, firstView.bottom+5, self.view.width, 120)];
    secondView.backgroundColor = [UIColor whiteColor];
    [scrollView addSubview:secondView];

    DCLabelAndTextLabel *sendNameLabel = [[DCLabelAndTextLabel alloc]initWithFrame:CGRectMake(0, 0, secondView.width - 20, secondView.height/3)];
    sendNameLabel.titleLabel.text = @"发货人";
    sendNameLabel.textField.text = @"张三";
    [secondView addSubview:sendNameLabel];

    DCLabelAndTextLabel *phoneNumLabel = [[DCLabelAndTextLabel alloc]initWithFrame:CGRectMake(sendNameLabel.x, sendNameLabel.bottom, sendNameLabel.width, sendNameLabel.height)];
    phoneNumLabel.titleLabel.text = @"联系方式";
    phoneNumLabel.textField.text = @"12345678910";
    [secondView addSubview:phoneNumLabel];

    CGFloat buttonWidth = 20;
    UIButton *tihuoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    tihuoBtn.frame = CGRectMake(50*WIDTHSCALE6, phoneNumLabel.bottom+(sendNameLabel.height-buttonWidth)/2, buttonWidth, buttonWidth);
    [tihuoBtn setImage:IMAGENAME(@"方框") forState:UIControlStateNormal];
    [tihuoBtn setImage:IMAGENAME(@"选择-方框") forState:UIControlStateSelected];
    [tihuoBtn addTarget:self action:@selector(tihuoBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [secondView addSubview:tihuoBtn];
    UILabel *tihuoLabel = [[UILabel alloc]initWithFrame:CGRectMake(tihuoBtn.right+5, phoneNumLabel.bottom, 100*WIDTHSCALE6, sendNameLabel.height)];
    tihuoLabel.text = @"需要提货";
    [secondView addSubview:tihuoLabel];
    
    UIButton *fandanBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    fandanBtn.frame = CGRectMake(tihuoLabel.right+10, tihuoBtn.y, tihuoBtn.width, tihuoBtn.height);
    [fandanBtn setImage:IMAGENAME(@"方框") forState:UIControlStateNormal];
    [fandanBtn setImage:IMAGENAME(@"选择-方框") forState:UIControlStateSelected];
    [fandanBtn addTarget:self action:@selector(fandanBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [secondView addSubview:fandanBtn];
    UILabel *fandanLabel = [[UILabel alloc]initWithFrame:CGRectMake(fandanBtn.right+5, phoneNumLabel.bottom, tihuoLabel.width, sendNameLabel.height)];
    fandanLabel.text = @"需要返单";
    [secondView addSubview:fandanLabel];
    
    UIView *thirdView = [[UIView alloc]initWithFrame:CGRectMake(0, secondView.bottom+5, self.view.width, 120)];
    thirdView.backgroundColor = [UIColor whiteColor];
    [scrollView addSubview:thirdView];

    
    NSDictionary *dict = odd.receiverArray.count?odd.receiverArray[0]:nil;
    DCLabelAndTextLabel *receiveLabel = [[DCLabelAndTextLabel alloc]initWithFrame:CGRectMake(0, 0, thirdView.width-20, thirdView.height/3)];
    receiveLabel.titleLabel.text = @"收货人";
    receiveLabel.textField.text = dict[@"name"];
    [thirdView addSubview:receiveLabel];

    DCLabelAndTextLabel *receivePhoneLabel = [[DCLabelAndTextLabel alloc]initWithFrame:CGRectMake(0, receiveLabel.bottom, thirdView.width-20, thirdView.height/3)];
    receivePhoneLabel.titleLabel.text = @"联系方式";
    receivePhoneLabel.textField.text = dict[@"phoneNumber"];
    [thirdView addSubview:receivePhoneLabel];

    DCLabelAndTextLabel *receiveTimeLabel = [[DCLabelAndTextLabel alloc]initWithFrame:CGRectMake(0, receivePhoneLabel.bottom, thirdView.width, thirdView.height/3)];
    receiveTimeLabel.titleLabel.text = @"要求到货时间";
    receiveTimeLabel.textField.text = odd.endDate;
    receiveTimeLabel.line.hidden = YES;
    [thirdView addSubview:receiveTimeLabel];
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, thirdView.bottom+5, self.view.width, 30*odd.carArray.count+40) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [scrollView addSubview:self.tableView];
    
    UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, self.tableView.bottom+5, ScreenWidth, 240)];
    footerView.backgroundColor = [UIColor whiteColor];
    [scrollView addSubview:footerView];
    
    NSArray *dataSource = [DCGisticsModel mj_objectArrayWithKeyValuesArray:self.model.orderTrack[@"nodeArray"]];
    DCGisticsView *gisticsView = [[DCGisticsView alloc]initWithSourceData:dataSource state:self.state];
    gisticsView.backgroundColor = [UIColor whiteColor];
    gisticsView.frame = CGRectMake(0, self.tableView.bottom+5, ScreenWidth, (dataSource.count+1)*40);
    [scrollView addSubview:gisticsView];
    
    scrollView.contentSize = CGSizeMake(ScreenWidth, CGRectGetMaxY(gisticsView.frame)+5);
}

-(void)tihuoBtnDidClick:(UIButton *)button
{
    button.selected = !button.selected;
}
-(void)fandanBtnDidClick:(UIButton *)button
{
    button.selected = !button.selected;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    DCOrderDetailOdd *odd = self.model.oddArray[0];
    return odd.carArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DCOrderDetailCell *cell = [DCOrderDetailCell cellWithTableView:tableView];
    DCOrderDetailOdd *odd = self.model.oddArray[0];
    cell.model = odd.carArray[indexPath.row];
    return cell;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc]init];
//    view.backgroundColor = [UIColor redColor];
    DCImageAndTextLabel *suvLabel = [[DCImageAndTextLabel alloc]initWithFrame:CGRectMake(10, 0, ScreenWidth/3, 40)];
//    suvLabel.backgroundColor = [UIColor cyanColor];
    suvLabel.imageView.image = IMAGENAME(@"SUV");
    suvLabel.textLabel.text = @"3台";
    suvLabel.textLabel.textColor = [UIColor redColor];
    [view addSubview:suvLabel];
    
    DCImageAndTextLabel *carLabel = [[DCImageAndTextLabel alloc]initWithFrame:CGRectMake(suvLabel.right, 0, suvLabel.width, suvLabel.height)];
//    carLabel.backgroundColor = [UIColor yellowColor];
    carLabel.imageView.image = IMAGENAME(@"car");
    carLabel.textLabel.text = @"3台";
    carLabel.textLabel.textColor = [UIColor redColor];
    [view addSubview:carLabel];
    
    UILabel *totalLabel = [[UILabel alloc]initWithFrame:CGRectMake(carLabel.right, 0, suvLabel.width, suvLabel.height)];
//    totalLabel.backgroundColor = [UIColor redColor];
    totalLabel.attributedText = [YLAttributedStr changNumberColorWithTotalString:@"共计   6台" andColor:[UIColor redColor]];
    [view addSubview:totalLabel];
    return view;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 40;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 30;
}
/*u
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
